//
// Created by daiyizheng on 2022/3/27.
//
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// 前序遍历 + 串接节点
void dfs(TreeNode* node, vector<TreeNode*>& res) {
    if (node == nullptr) return;
    res.push_back(node);
    dfs(node->left, res);
    dfs(node->right, res);
}
void flatten(TreeNode* root) {
    vector<TreeNode*> res;
    dfs(root, res);

    TreeNode *curr = root;
    for (int i = 1; i < res.size(); i++) {
        curr->left = nullptr;
        curr->right = res[i];
        curr = curr->right;
    }
}

